package cn.com.kblog.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

import cn.com.kblog.common.Constant;
import cn.com.kblog.model.Blog;
import cn.com.kblog.model.BlogType;
import cn.com.kblog.model.Tag;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 首页Controller
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-4下午2:40:56
 */
@ControllerBind(controllerKey = "/",viewPath="WEB-INF/pages")
public class IndexController extends Controller{
	
	/**
	 * 首页
	 */
	public void index(){
		
		_common();
		Map<String, String> params = new HashMap<String, String>();
		params.put(Blog.STATUS, Blog.STATUS_1);
		//页面号
		Integer pageNumber;
		try {
			pageNumber = getParaToInt(0,Constant.PAGE_NUM_DEFAULT);
		} catch (Exception e) {
			pageNumber = Constant.PAGE_NUM_DEFAULT;
		}
		
		//一页条数
		Integer pageSize = getParaToInt("pageSize", Constant.PAGE_SIZE_DEFAULT);
		//排序相关
		String orderField = Blog.UPDATE_TIME;
		String orderDirection = getPara("orderDirection", Constant.PAGE_ORDER_DIRECTION_DEFAULT);
		
		Page<Blog> beans = Blog.dao.pageQuery(pageNumber, pageSize, orderField, orderDirection, params);
		setAttr(Constant.BEANS_KEY, beans);
		render("index.html");
	}
	
	
	/**
	 * 公共属性设置
	 */
	private void _common(){
		List<Record> tags = Tag.dao.listTop(Constant.TAG_TOP_LIMIT);
		setAttr("top_tags", tags);
		List<BlogType> blogTypes = BlogType.dao.listAll();
		List<Blog> topBlogs = Blog.dao.listTopView();
		setAttr("top_blogs", topBlogs);
		setAttr("blogtypes", blogTypes);
	}
}
